package com.iphanta.community.activity.photographic.service.impl;

import com.iphanta.community.activity.photographic.common.BallBox;
import com.iphanta.community.activity.photographic.common.BallBoxCollection;
import com.iphanta.community.activity.photographic.common.BallBoxSelector;
import com.iphanta.community.activity.photographic.service.PhotographicService;
import com.iphanta.community.activity.photographic.traffic.vo.ActivityDroplet;
import com.iphanta.community.cache.ActivityCache;
import com.iphanta.community.common.BusinessException;
import com.iphanta.community.common.UserUtils;
import com.iphanta.community.common.utils.RedisKeyUtils;
import com.iphanta.community.common.utils.ResultUtils;
import com.iphanta.community.following.service.DynamicService;
import com.iphanta.community.following.traffic.DynamicFlowResponse;
import com.iphanta.community.following.traffic.vo.Droplet;
import org.redisson.api.*;
import org.redisson.api.mapreduce.RCollectionMapReduce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 墨声 E-mail: zehong.hongframe.huang@gmail.com
 * create time: 4/13/22 11:12 AM
 */
@Service
public class PhotographicServiceImpl implements PhotographicService {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private ActivityCache activityCache;

    @Override
    public List<ActivityDroplet> take(String activityId) {

        BallBoxCollection collection = (BallBoxCollection) this.activityCache.get(activityId);

        BitSet bitmap = this.redissonClient
                .getBitSet(RedisKeyUtils.INSTANCE
                        .key("bitmap", activityId, UserUtils.getUid()))
                .asBitSet();

        List<BallBox> ballBoxes = collection.allBox();
        ballBoxes.stream().filter(e -> !bitmap.get(e.getEntryId().intValue())).collect(Collectors.toList());

        BallBoxSelector selector = new BallBoxSelector(ballBoxes, new Random(System.currentTimeMillis()));
        List<Droplet> droplets = this.dynamicService.query(selector.choice(10));
        List<ActivityDroplet> activityDroplets = null;

        return null;
    }


    private List<ActivityDroplet> activityDroplets(List<Droplet> droplets, BallBoxCollection collection) {
        List<ActivityDroplet> activityDroplets = new ArrayList<>();
        for(Droplet droplet : droplets) {
//            activityDroplets.add(new ActivityDroplet(droplet, this.activityCache.))
        }
        return null;
    }


    public void glide(String activityId, Long dynamicId, String entityId) {

        BallBoxCollection collection = (BallBoxCollection) this.activityCache.get(activityId);

        BallBox ballBox = collection.get(dynamicId);
        ballBox.exposure(this.redissonClient.createTransaction(TransactionOptions.defaults()));

    }
}
